<?php
/**
 * Descript:
 * User: lufeng501206@gmail.com
 * Date: 2017/3/7 22:04
 */

namespace App\Http\Controllers\Admin;


use App\Http\Controllers\Controller;
use Illuminate\Http\Request;

class IndexController extends Controller
{
    public function __construct()
    {
    }

    public function index()
    {
        return view('system.tasks-manager');
    }

    /**
     * 登录页面
     * @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View
     */
    public function login()
    {
        return view('system.login');
    }

    private function checkUser($user,$password){
        $userLists = config('jinbell.users');
        $userLists = rebulidDataIndex($userLists,'name');
        if (!empty($userLists[$user]) && $userLists[$user]['password'] == $password) {
            return $userLists[$user]['auth'];
        }else{
            return false;
        }
    }

    /**
     * @param \Request $request
     */
    public function doLogin(Request $request)
    {
        $inputs = $request->input();
        $user = $inputs['user'];
        $password = $inputs['password'];
        if ($user == env('SUPER_ADMIN') && $password == env('SUPER_PASSWORD')) {
            $request->session()->put('account', $user);
            return successJson();
        } elseif ($this->checkUser($user,$password)) {
            $request->session()->put('account', $user);
            return successJson();
        } else {
            return errorJson('','用户名或密码错误！');
        }
    }

    public function register()
    {
        return view('system.register');
    }
}